package com.smart.seckilldemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.seckilldemo.commons.Parameters;
import com.smart.seckilldemo.commons.vo.SeckillGoodsVo;
import com.smart.seckilldemo.mapper.GoodMapper;
import com.smart.seckilldemo.pojo.Good;
import com.smart.seckilldemo.pojo.SeckillGoods;
import com.smart.seckilldemo.service.GoodsService;
import com.smart.seckilldemo.service.SeckillGoodsService;
import com.smart.seckilldemo.utils.RedisJudgeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Resource
    GoodMapper goodMapper;
    @Resource
    SeckillGoodsService seckillGoodsService;
    @Resource
    RedisJudgeUtils redisJudgeUtils;

    /**
     * 获取商品详情信息 带秒杀信息
     * @param goodId
     * @return
     */
    @Override
    public SeckillGoodsVo findGoodByGoodId(Long goodId) {
        String redisKey= Parameters.SeckillGoodsDateCacheVo+":"+goodId;
        SeckillGoodsVo seckillGoodsVo = null;
        try {
            seckillGoodsVo = (SeckillGoodsVo) redisJudgeUtils.getRed(redisKey);
            if(seckillGoodsVo==null){
                seckillGoodsVo = seckillGoodsService.finSeckillGoodByGoods(goodId);
                redisJudgeUtils.setRed(redisKey,seckillGoodsVo);
            }
        }catch (Exception e){
            log.info("商品详情获取出错");
        }
        return seckillGoodsVo;
    }

    /**
     * 获取商品列表信息
     * @param current
     * @return
     */
    @Override
    public Page<Good> findGoodList(Long current) {
        String redisKey=Parameters.GoodsList+":"+current;
        Page<Good> goodPage=null;
        try {
            Page<Good> red = (Page<Good>) redisJudgeUtils.getRed(redisKey);
            if(red!=null){
                goodPage=red;
            }else {
                QueryWrapper<Good> qw = new QueryWrapper<>();
                qw.eq(Good.COL_DELETE_TYPE,1);
                Page<Good> page = new Page<>(current,10);
                goodPage = goodMapper.selectPage(page, qw);
                if(goodPage!=null){
                    //存入缓存
                    redisJudgeUtils.setRed(redisKey,goodPage);
                }
            }

        }catch (Exception e){
            log.info("获取商品列表错误");
        }
        return goodPage;
    }

    @Override
    public List<Good> findSeckillGood() {
        String redisKey=Parameters.GoodsDate;
        List<Good> list=null;
        try {
            List<Good> red = (List<Good>) redisJudgeUtils.getRed(redisKey);
            if(red!=null){
                list=red;
            }else {
                list = goodMapper.findSeckillGood();
                if(list!=null){
                    //存入缓存
                    redisJudgeUtils.setRed(redisKey,list);
                }
            }
        }catch (Exception e){
            log.info("获取商品列表错误");
        }
        return list;
    }




}
